﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using SRM;
using SRM.DataObjects;

public partial class UserControls_EmploymentHistoryUserControl : System.Web.UI.UserControl, SRM.IUserInput
{
    public DataTable PersonalParticularsDataTable { get; set; }

    public DataRow row { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            this.row = null;
            //Page.DataBind();

            int applicantKey = int.Parse(Session["ApplicantKey"].ToString());
            this.PersonalParticularsDataTable = SRM_Applicant.GetPersonalParticulars(applicantKey.ToString());
            if ((this.PersonalParticularsDataTable != null) && (this.PersonalParticularsDataTable.Rows.Count > 0))
                this.row = this.PersonalParticularsDataTable.Rows[0];

            if (this.row == null)
                return;

            // Start filling in details on the form
            this.freshGraduateRadioButtonList.SelectedValue = this.row.Field<string>("FreshGraduate");
            if (freshGraduateRadioButtonList.SelectedValue.Equals("No", StringComparison.InvariantCultureIgnoreCase))
            {
                rgEmploymentHistory.Visible = true;
            }
            else
            {
                rgEmploymentHistory.Visible = false;
            }
        }
    }

    private bool ValidateFormFields(string mode)
    {
        bool hasFilledAllRequiredFields = true;

        if (mode == "UPDATE")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["currentJob"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["startingPosition"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["currentPosition"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["employer"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["nature"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["country"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["periodFrom"].DefaultValue)))
                hasFilledAllRequiredFields = false;
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["periodTo"].DefaultValue)))
            {
                if (EmploymentHistory_ObjectDataSource.UpdateParameters["currentJob"].DefaultValue != "YES")
                {
                    hasFilledAllRequiredFields = false; 
                }
            }
                
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["startingBasicSalary"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["currentBasicSalary"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.UpdateParameters["reasonLeaving"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
        }

        if (mode == "INSERT")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["currentJob"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["startingPosition"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["currentPosition"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["employer"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["nature"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["country"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["periodFrom"].DefaultValue)))
                hasFilledAllRequiredFields = false;
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["periodTo"].DefaultValue)))
            {
                if (EmploymentHistory_ObjectDataSource.InsertParameters["currentJob"].DefaultValue != "YES")
                {
                    hasFilledAllRequiredFields = false;
                }
            }
                
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["startingBasicSalary"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["currentBasicSalary"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(EmploymentHistory_ObjectDataSource.InsertParameters["reasonLeaving"].DefaultValue)))
                hasFilledAllRequiredFields = false; 

        }

        return hasFilledAllRequiredFields;
    }

    protected void rgEmploymentHistory_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            EmploymentHistory_ObjectDataSource.UpdateParameters["empHistoryKey"].DefaultValue = SRM.Help.GetHiddenFieldValue(item.FindControl("empHistoryKey"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["employer"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("employer_textbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["nature"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("industrytypecode_combobox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["periodFrom"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("periodfrom_datepicker"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["periodTo"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("periodto_datepicker"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["startingPosition"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("startingposition_textbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["startingBasicSalary"].DefaultValue = SRM.Help.GetRadNumericTextBoxValue(item.FindControl("startingbasicsalary_numerictextbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["currentPosition"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("currentposition_textbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["currentBasicSalary"].DefaultValue = SRM.Help.GetRadNumericTextBoxValue(item.FindControl("currentbasicsalary_numerictextbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["allowances"].DefaultValue = SRM.Help.GetRadNumericTextBoxValue(item.FindControl("allowances_numerictextbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["aws"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("aws_combobox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["bonus"].DefaultValue = SRM.Help.GetRadNumericTextBoxValue(item.FindControl("bonus_numerictextbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["jobDuties"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("jobduties_textbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["reasonLeaving"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("reasonleaving_textbox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["currentJob"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("currentjob_combobox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["industryTypeCode"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("industrytypecode_combobox"));
            EmploymentHistory_ObjectDataSource.UpdateParameters["country"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("country_combobox"));
            

            if (ValidateFormFields("UPDATE"))
            {
                EmploymentHistory_ObjectDataSource.Update();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("EmploymentHistoryMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            EmploymentHistory_ObjectDataSource.DeleteParameters["empHistoryKey"].DefaultValue = item["EmpHistoryKey_column"].Text;
            EmploymentHistory_ObjectDataSource.Delete();
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            EmploymentHistory_ObjectDataSource.InsertParameters["employer"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("employer_textbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["nature"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("industrytypecode_combobox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["periodFrom"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("periodfrom_datepicker"));
            EmploymentHistory_ObjectDataSource.InsertParameters["periodTo"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("periodto_datepicker"));
            EmploymentHistory_ObjectDataSource.InsertParameters["startingPosition"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("startingposition_textbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["startingBasicSalary"].DefaultValue = SRM.Help.GetRadNumericTextBoxValue(item.FindControl("startingbasicsalary_numerictextbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["currentPosition"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("currentposition_textbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["currentBasicSalary"].DefaultValue = SRM.Help.GetRadNumericTextBoxValue(item.FindControl("currentbasicsalary_numerictextbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["allowances"].DefaultValue = SRM.Help.GetRadNumericTextBoxValue(item.FindControl("allowances_numerictextbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["aws"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("aws_combobox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["bonus"].DefaultValue = SRM.Help.GetRadNumericTextBoxValue(item.FindControl("bonus_numerictextbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["jobDuties"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("jobduties_textbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["reasonLeaving"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("reasonleaving_textbox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["currentJob"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("currentjob_combobox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["industryTypeCode"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("industrytypecode_combobox"));
            EmploymentHistory_ObjectDataSource.InsertParameters["country"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("country_combobox"));

            if (ValidateFormFields("INSERT"))
            {
                EmploymentHistory_ObjectDataSource.Insert();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("EmploymentHistoryMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }
    }

    protected void rgEmploymentHistory_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            DataTable dt = null;
            GridEditFormItem geiEditedItem = e.Item as GridEditFormItem;
            geiEditedItem.Visible = true;

            //Telerik.Web.UI.RadComboBox industrytypecode_combobox = null;
            //Telerik.Web.UI.RadComboBox country_combobox = null;

            System.Web.UI.WebControls.TextBox employer_textbox = null;
            Telerik.Web.UI.RadDatePicker periodfrom_datepicker = null;
            Telerik.Web.UI.RadDatePicker periodto_datepicker = null;
            System.Web.UI.WebControls.TextBox startingposition_textbox = null;
            Telerik.Web.UI.RadNumericTextBox startingbasicsalary_numerictextbox = null;
            System.Web.UI.WebControls.TextBox currentposition_textbox = null;
            Telerik.Web.UI.RadNumericTextBox currentbasicsalary_numerictextbox = null;
            Telerik.Web.UI.RadNumericTextBox allowances_numerictextbox = null;
            Telerik.Web.UI.RadComboBox aws_combobox = null;
            Telerik.Web.UI.RadNumericTextBox bonus_numerictextbox = null;
            System.Web.UI.WebControls.TextBox jobduties_textbox = null;
            System.Web.UI.WebControls.TextBox reasonleaving_textbox = null;
            Telerik.Web.UI.RadComboBox currentjob_combobox = null;
            Telerik.Web.UI.RadComboBox industrytypecode_combobox = null;
            Telerik.Web.UI.RadComboBox country_combobox = null;
            System.Web.UI.HtmlControls.HtmlGenericControl periodto_star = null;

            currentjob_combobox = geiEditedItem.FindControl("currentjob_combobox") as Telerik.Web.UI.RadComboBox;
            startingposition_textbox = geiEditedItem.FindControl("startingposition_textbox") as System.Web.UI.WebControls.TextBox;
            currentposition_textbox= geiEditedItem.FindControl("currentposition_textbox") as System.Web.UI.WebControls.TextBox;

            jobduties_textbox = geiEditedItem.FindControl("jobduties_textbox") as System.Web.UI.WebControls.TextBox;
            periodfrom_datepicker = geiEditedItem.FindControl("periodfrom_datepicker") as Telerik.Web.UI.RadDatePicker;
            periodto_datepicker = geiEditedItem.FindControl("periodto_datepicker") as Telerik.Web.UI.RadDatePicker;
            startingbasicsalary_numerictextbox = geiEditedItem.FindControl("startingbasicsalary_numerictextbox") as Telerik.Web.UI.RadNumericTextBox;
            currentbasicsalary_numerictextbox = geiEditedItem.FindControl("currentbasicsalary_numerictextbox") as Telerik.Web.UI.RadNumericTextBox;

            allowances_numerictextbox = geiEditedItem.FindControl("allowances_numerictextbox") as Telerik.Web.UI.RadNumericTextBox;
            aws_combobox = geiEditedItem.FindControl("aws_combobox") as Telerik.Web.UI.RadComboBox;
            bonus_numerictextbox = geiEditedItem.FindControl("bonus_numerictextbox") as Telerik.Web.UI.RadNumericTextBox;
            reasonleaving_textbox= geiEditedItem.FindControl("reasonleaving_textbox") as System.Web.UI.WebControls.TextBox;

            employer_textbox = geiEditedItem.FindControl("employer_textbox") as System.Web.UI.WebControls.TextBox;
            industrytypecode_combobox = geiEditedItem.FindControl("industrytypecode_combobox") as Telerik.Web.UI.RadComboBox;
            country_combobox = geiEditedItem.FindControl("country_combobox") as Telerik.Web.UI.RadComboBox;

            periodto_star = geiEditedItem.FindControl("periodto_star") as System.Web.UI.HtmlControls.HtmlGenericControl;

            

            // Databind the 2 combo-boxes, industrytypecode_combobox
            Help.BindSapCodeToComboBox(industrytypecode_combobox, SRM_Code.GetIndustry("1"));
            Help.BindSapCodeToComboBox(country_combobox, SRM_Code.GetCountry("1"));

            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {
                System.Data.DataRow row = ((DataRowView)e.Item.DataItem).Row;

                //currentjob_combobox.SelectedValue = row.Field<string>("CurrentJob");
                //aws_combobox.SelectedValue = row.Field<string>("AWS");
                Help.SetComboBoxSelectedValue(currentjob_combobox, row.Field<string>("CurrentJob"));

                if (currentjob_combobox.SelectedValue != "YES")
                {
                    periodto_star.Visible = true;
                }
                else
                {
                    periodto_star.Visible = false;
                }

                Help.SetComboBoxSelectedValue(aws_combobox, row.Field<string>("AWS"));

                startingposition_textbox.Text = row.Field<string>("StartingPosition");
                currentposition_textbox.Text = row.Field<string>("CurrentPosition");

                jobduties_textbox.Text = row.Field<string>("JobDuties");
                periodfrom_datepicker.SelectedDate = row.Field<DateTime?>("PeriodFrom");
                periodto_datepicker.SelectedDate = row.Field<DateTime?>("PeriodTo");
                startingbasicsalary_numerictextbox.Value = Help.GetNullableDouble(row.Field<string>("StartingBasicSalary"));
                currentbasicsalary_numerictextbox.Value = Help.GetNullableDouble(row.Field<string>("CurrentBasicSalary"));

                allowances_numerictextbox.Text = row.Field<string>("Allowances");
                aws_combobox.Text = row.Field<string>("AWS");
                bonus_numerictextbox.Text = row.Field<string>("Bonus");
                reasonleaving_textbox.Text = row.Field<string>("ReasonLeaving");

                employer_textbox.Text = row.Field<string>("Employer");
                //industrytypecode_combobox.SelectedValue = row.Field<string>("IndustryTypeCode");
                //country_combobox.SelectedValue = row.Field<string>("Country");
                Help.SetComboBoxSelectedValue(industrytypecode_combobox, row.Field<string>("IndustryTypeCode"));
                Help.SetComboBoxSelectedValue(country_combobox, row.Field<string>("Country"));

                //industrytypecode_combobox.SelectedValue = ((DataRowView)e.Item.DataItem).Row["HobbyCategory"].ToString();
                //country_combobox.SelectedValue = ((System.Data.DataRowView)e.Item.DataItem).Row["HobbyDescription"].ToString();

            }
        }
    }

    protected void freshGraduateRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (freshGraduateRadioButtonList.SelectedValue.Equals("Yes", StringComparison.InvariantCultureIgnoreCase))
        {
            rgEmploymentHistory.Visible = false;
        }
        else
        {
            rgEmploymentHistory.Visible = true;
        }
    }

    public void SaveUserInput()
    {
        string applicantKey = string.Empty;

        if (Page is IApplicantForm)
        {
            applicantKey = ((IApplicantForm)Page).ApplicantKey;

            SRM_Applicant.UpdateFreshGraduateStatus(applicantKey,
                this.freshGraduateRadioButtonList.SelectedValue);
        }
    }

    protected void currentjob_combobox_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        Telerik.Web.UI.RadComboBox currentjob_combobox =
            ((Telerik.Web.UI.RadComboBox)o).NamingContainer.FindControl("currentjob_combobox") as Telerik.Web.UI.RadComboBox;
        System.Web.UI.HtmlControls.HtmlGenericControl periodto_star =
            ((Telerik.Web.UI.RadComboBox)o).NamingContainer.FindControl("periodto_star") as System.Web.UI.HtmlControls.HtmlGenericControl;

        if (e.Value != "YES")
        {
            periodto_star.Visible = true;
        }
        else
        {
            periodto_star.Visible = false;
        }
        
    }
}